package fr.esiag.isidis.epp.mock.util;

import java.util.Vector;




import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.jvnet.jaxb2_commons.lang.StringUtils;
import org.mockito.Mock;

import static org.mockito.Mockito.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;

import fr.esiag.isidis.epp.model.CDCModel;
import fr.esiag.isidis.epp.repository.CDCAndWeightRepository;
import fr.esiag.isidis.epp.repository.SqlConstants;
import fr.esiag.isidis.epp.util.Utils;

public class test_mock {
	
private static Logger logger = Logger.getLogger(CDCAndWeightRepository.class);
	
	@Value("${batch.in.cdc.file}")
	private String cdc_pathCsvFile;
	
	@Value("${batch.in.weight.file}")
	private String weight_pathCsvFile;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	CDCModel cdcItem;
	utils utl;
	
	@Before
	public void startup() {
		cdcItem =new CDCModel();
		 utl=mock(utils.class);
	}
	
	@Test
	
	public int LoadCdc(CDCModel cdcItem)
	{
		
		
		/* traitement cdc */
		
		int date= utl.convertDateToInt(cdcItem.getDates());
		when(utl.convertDateToIntSite(anyString())).thenReturn(Integer.valueOf(01022015));			
		when(utl.convertDateToIntSite(StringUtils.EMPTY)).thenReturn(-1);
		
		int heure= utl.convertDateToInt(cdcItem.getDates());
		when(utl.convertDateToIntSite(anyString())).thenReturn(Integer.valueOf(2));			
		when(utl.convertDateToIntSite(StringUtils.EMPTY)).thenReturn(-1);
		
		int id;
		String idnn;
		int charge;
		Vector<Integer> tabCharge = cdcItem.getCharge();
		// weight
		int result = 0;
		for(id=0; id< 42; id++){
			idnn = Utils.getIdnn(id);
			if(tabCharge.get(id)!= null){
				charge = tabCharge.get(id);
				Object[] params = {idnn,date,heure,charge,null};
				result += jdbcTemplate.update(SqlConstants.QUEYR_INSERT_INTO_CDC_WEIGHT, params);
			}
			else{
				Object[] params = {idnn,date,heure,null,null};
				result += jdbcTemplate.update(SqlConstants.QUEYR_INSERT_INTO_CDC_WEIGHT, params);
			}
			
		}
		
		return result;
		
		
	}

	public void testLoad(){
		
		cdcItem.setDates("07-05-1992");
		LoadCdc(cdcItem);
		Assert.assertEquals("07-05-1992",utl.convertDateToInt("07-05-1992"));
		
		
		
	}
	
	

}
